// $Id: NavigatorView.java 56 2011-11-11 22:41:06Z huijzer@gmail.com $
package lime.browser.view;

import lime.browser.editor.DocumentEditor;
import lime.browser.editor.DocumentInput;
import lime.browser.provider.NavigatorProvider;
import lime.common.LimeException;
import lime.model.Cabinet;
import lime.model.Document;
import lime.model.Folder;
import lime.model.NavigatorRoot;
import lime.service.RepositoryService;
import lime.service.SpringApplicationContext;

import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.model.WorkbenchLabelProvider;
import org.eclipse.ui.part.ViewPart;

import com.documentum.fc.client.IDfFolder;
import com.documentum.fc.client.IDfSysObject;
import com.documentum.fc.common.DfException;
import com.documentum.fc.common.DfId;
import com.documentum.fc.common.IDfId;

public class NavigatorView extends ViewPart {

	public static final String ID = "lime.browser.view.navigator";

	// Services
	RepositoryService repositoryService;

	// Widgets
	TreeViewer viewer;

	@Override
	public void init(IViewSite site) throws PartInitException {
		super.init(site);
		repositoryService = (RepositoryService) SpringApplicationContext.getBean("repositoryService");
	}

	@Override
	public void createPartControl(Composite parent) {

		viewer = new TreeViewer(parent, SWT.None);
		viewer.setContentProvider(new NavigatorProvider());
		viewer.setLabelProvider(new WorkbenchLabelProvider());
		viewer.setInput(new NavigatorRoot());

		// Add context menu
		MenuManager menuManager = new MenuManager();
		menuManager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
		viewer.getTree().setMenu(menuManager.createContextMenu(viewer.getTree()));
		getSite().registerContextMenu(menuManager, viewer);
		getSite().setSelectionProvider(viewer);

		viewer.addDoubleClickListener(new IDoubleClickListener() {
			@Override
			public void doubleClick(DoubleClickEvent event) {
				ISelection selection = event.getSelection();
				if (selection != null && selection instanceof IStructuredSelection) {
					IStructuredSelection structuredSelection = (IStructuredSelection) selection;
					Object element = structuredSelection.getFirstElement();
					if (element instanceof Document) {
						Document document = (Document) element;
						DocumentInput documentInput = new DocumentInput(document);
						try {
							getSite().getPage().openEditor(documentInput, DocumentEditor.ID);
						} catch (PartInitException e) {
							throw new LimeException(e);
						}
					}
				}
			}
		});

	}

	public TreeViewer getViewer() {
		return viewer;
	}

	@Override
	public void setFocus() {
		this.viewer.getTree().setFocus();

		// Get the status line and set the text
		String message = repositoryService.getConnectionInfo();
		getViewSite().getActionBars().getStatusLineManager().setMessage(message);

	}

	public void showObjectInTree(String id) {

		try {

			IDfId objectId = new DfId(id);
			IDfSysObject doc = (IDfSysObject) repositoryService.getSession().getObject(objectId);
			IDfFolder folder = (IDfFolder) repositoryService.getSession().getObject(doc.getFolderId(0));

			int lastIndex = folder.getAncestorIdCount() - 1;

			Cabinet cabinet = new Cabinet(folder.getAncestorId(lastIndex), "");
			TreePath path = new TreePath(new Object[] { cabinet });

			for (int i = lastIndex - 1; i >= 0; i--) {
				String ancestorId = folder.getAncestorId(i);
				Folder newFolder = new Folder(ancestorId, "");
				path = path.createChildPath(newFolder);
			}

			Document document = new Document(doc.getObjectId().getId(), "");
			path = path.createChildPath(document);

			viewer.setExpandedTreePaths(new TreePath[] { path });
			TreeSelection selection = new TreeSelection(path);
			viewer.setSelection(selection);

			viewer.getTree().setFocus();

		} catch (DfException e) {
			throw new LimeException(e);
		}

	}

}
